/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.service.system.impl;

import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.dao.system.RoleDao;
import org.grayrabbit.cms.dao.system.RoleOperRelaDao;
import org.grayrabbit.cms.entity.Admin;
import org.grayrabbit.cms.entity.Role;
import org.grayrabbit.cms.entity.RoleOperRela;
import org.grayrabbit.cms.service.category.impl.CategoryCache;
import org.grayrabbit.cms.service.system.RoleOperRelaService;
import org.grayrabbit.cms.util.OperCodeJSONUtil;
import org.grayrabbit.cms.vo.system.RoleOperRelaBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.service.impl.GenericServiceImpl;
/**
 * <b>RoleOperRela</b>的service实现类，事务加此处：





 * @version 1.0,创建时间: 2011-05-16 15:55:12  
 */
@Service
@Transactional
public class RoleOperRelaServiceImpl extends GenericServiceImpl implements RoleOperRelaService {
	/**
	 * 注入dao层：
	 */
	@Autowired
	@Qualifier("roleOperRelaDaoImpl")
	private RoleOperRelaDao roleOperRelaDao;
	
	@Autowired
	@Qualifier("roleDaoImpl")
	private RoleDao roleDao;
	
	/**
	 * 此方法执行时为只读事务：
	 * @see RoleOperRelaService#getRoleOperRela(String)
	 */
	@Transactional(readOnly=true)
	public RoleOperRela getRoleOperRela(final String roleOperRelaId){
		RoleOperRela roleOperRela = roleOperRelaDao.findById(roleOperRelaId);
		roleOperRelaDao.initialize(roleOperRela);
		return roleOperRela;
	}
	
	/**
	 * @see RoleOperRelaService#delete(String)
	 */
	@Transactional
	public void delete(final RoleOperRela roleOperRela) {
		roleOperRelaDao.makeTransient(roleOperRela);
	}
	
	/**
	 * @see RoleOperRelaService#save(RoleOperRela)
	 */
	@Transactional
	public void save(final RoleOperRela roleOperRela) {
		roleOperRelaDao.makePersistent(roleOperRela);		
	}
	
	/**
	 * 此方法执行时为只读事务：
	 * @see RoleOperRelaService#getPage(roleOperRelaBean)
	 */
	@Transactional(readOnly=true)
	public Page<RoleOperRela> getPage(final RoleOperRelaBean roleOperRelaBean){
		Page<RoleOperRela> page =  new Page<RoleOperRela>();
		page.setTotal(roleOperRelaDao.count(roleOperRelaBean));
		page.setResult(roleOperRelaDao.findByQc(roleOperRelaDao.assembly(roleOperRelaBean)));
		return page;
	}

	/**
	 * 此方法执行时为只读事务：
	 * @see RoleOperRelaService#list(Page, RoleOperRelaBean)
	 */
	public void list(Page<RoleOperRela> page, RoleOperRelaBean roleOperRelaBean) {
		if(page==null)
			return;
		if(page.isAutoCount()){
			page.setTotal(roleOperRelaDao.count(roleOperRelaBean));
		}
		page.setResult(roleOperRelaDao.findByPage(page,roleOperRelaBean));
	}
	
	/**
	 * 根据主键id列删除，批量删除RoleOperRela
	 * @see RoleOperRelaService#delete(String[])
	 */
	public void delete(final String[] key) {
		int i =0;
		for(String id : key){
			roleOperRelaDao.makeTransient(roleOperRelaDao.findById(id));
			i++;
			if(i==MAX_CLEAR)
				roleOperRelaDao.clear();
		}
		
	}
	@Transactional
	public void save(List<RoleOperRela> relaList) {
		 for(RoleOperRela rela:relaList){
			 roleOperRelaDao.makePersistent(rela);
		 } 
	}
	@Transactional(readOnly=true)
	public List<RoleOperRela> findAllAuthCodeByRoleId(String roleId) {
		List<RoleOperRela> list = roleOperRelaDao.findAllAuthCodeByRoleId(roleId);
		if(list != null && !list.isEmpty()){
			for(RoleOperRela r :list){
				roleOperRelaDao.initialize(r.getOperCode());
			}
		} 
		return list;
	}
	
	@Transactional(readOnly=true)
	public String wrapAuthJsonText(String objectId){
		List<RoleOperRela> list = findAllAuthCodeByRoleId(objectId);		
		return  OperCodeJSONUtil.fromRoleOperRelaToJSON(list);
	}
 
	@Transactional
	public void saveAllAuthCodeByRoleId(String roleId,String objectType ,String authJsonText){
		List<RoleOperRela> list = OperCodeJSONUtil.getRoleAuth(authJsonText, roleId, objectType);
		Role role = roleDao.findById(roleId);
		for(Admin admin : role.getAdmins()){
			AdminContext.remove(admin.getLoginName());//清除用户缓存
			CategoryCache.removeAuthCatsAndFlagbyAdminIds(admin.getAdminId());//清除用户栏目缓存
		}
		makeTransientRoleAuth(roleId);
		save(list);
	}

	public void deleteObjectAuth(String[] targetIds, String targetType) {
		if(targetIds == null || ArrayUtils.isEmpty(targetIds) || StringUtils.isBlank(targetType))
			   throw new IllegalArgumentException(" targetIds or targetType   can't be  blank or empty!");
		for(String targetId:targetIds){			
			List<RoleOperRela>  list = roleOperRelaDao.findObjectAuth(targetId, targetType);	
			for(RoleOperRela r:list){ 
				String ids = OperCodeJSONUtil.delteTargetIds(r.getOperTargetId(), targetId);
				String ids2 = OperCodeJSONUtil.delteTargetIds(r.getOperTargetId2(), targetId);
				String ids3 = OperCodeJSONUtil.delteTargetIds(r.getOperTargetId3(), targetId);
				String ids4 = OperCodeJSONUtil.delteTargetIds(r.getOperTargetId4(), targetId);
				String ids5 = OperCodeJSONUtil.delteTargetIds(r.getOperTargetId5(), targetId);
				if(StringUtils.isBlank(ids) && StringUtils.isBlank(ids2) && StringUtils.isBlank(ids3)
						&& StringUtils.isBlank(ids4) && StringUtils.isBlank(ids5)){
					roleOperRelaDao.makeTransient(r);	
				}else{
					r.setOperTargetId(ids);
					r.setOperTargetId2(ids2);
					r.setOperTargetId3(ids3);
					r.setOperTargetId4(ids4);
					r.setOperTargetId5(ids5);
					roleOperRelaDao.makePersistent(r);
				}
				for(Admin admin : r.getRole().getAdmins()){
					AdminContext.remove(admin.getLoginName());//清除用户缓存
					CategoryCache.removeAuthCatsAndFlagbyAdminIds(admin.getAdminId());//清除用户栏目缓存
				}
			}
		}
		
	} 
	
	public void deleteRoleAuth(String roleId) {
		makeTransientRoleAuth(roleId);
		Role role = roleDao.findById(roleId);
		roleDao.initialize(role.getAdmins());
		for(Admin admin : role.getAdmins()){
			AdminContext.remove(admin.getLoginName());//清除用户缓存
			CategoryCache.removeAuthCatsAndFlagbyAdminIds(admin.getAdminId());//清除用户栏目缓存
		}
	} 
	
	private void makeTransientRoleAuth(String roleId){
		if(StringUtils.isBlank(roleId) ) 
			   throw new IllegalArgumentException("roleId  can't be  blank!");
		List<RoleOperRela> relas = roleOperRelaDao.findAllAuthCodeByRoleId(roleId);
		for(RoleOperRela r : relas){
			roleOperRelaDao.makeTransient(r);
		}
	}
}